import cv2
import numpy as np


def calculate_gradient_at_point(image_path, point):
    """
    计算指定点周围的梯度信息。

    :param image_path: 图像的路径。
    :param point: 一个元组，表示要计算梯度的点的坐标(x, y)。
    """
    # 读取图像并转换为灰度图
    image = cv2.imread(image_path)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # 使用Sobel算子计算x和y方向的梯度
    sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
    sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)

    # 获取指定点的梯度值
    x, y = point
    grad_x = sobelx[y, x]
    grad_y = sobely[y, x]

    # 计算梯度幅度和方向
    magnitude = np.sqrt(grad_x ** 2 + grad_y ** 2)
    angle = np.arctan2(grad_y, grad_x) * (180 / np.pi)  # 转换为角度

    cv2.imshow('Sobel X', sobelx)
    cv2.imshow('Sobel Y', sobely)

    print(f"Point: {point}")
    print(f"Gradient Magnitude: {magnitude}")
    print(f"Gradient Angle (in degrees): {angle}")

    cv2.waitKey(0)
    cv2.destroyAllWindows()
# 示例：计算图像中(50, 50)点周围的梯度信息
calculate_gradient_at_point('imgs/birdview.jpg', (50, 50))